using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall.Math.Geometry;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Input;
using FlatRedBall.IO;
using FlatRedBall.Instructions;
using FlatRedBall.Math.Splines;
using FlatRedBall.Utilities;
using BitmapFont = FlatRedBall.Graphics.BitmapFont;

using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;

#if XNA4
using Color = Microsoft.Xna.Framework.Color;
#else
using Color = Microsoft.Xna.Framework.Graphics.Color;
#endif

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;
using Microsoft.Xna.Framework.Media;
#endif

// Generated Usings
using FlatRedBall.Broadcasting;
using MP3.Entities;
using FlatRedBall;
using FlatRedBall.Math;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Graphics;

namespace MP3.Screens
{
	public partial class GameScreen : Screen
	{
		// Generated Fields
		#if DEBUG
		static bool HasBeenLoadedWithGlobalContentManager = false;
		#endif
		private ShapeCollection CollisionFile;
		private ShapeCollection GoalAreaFile;
		private ShapeCollection Top2Walls;
		
		private PositionedObjectList<PlayerBall> mPlayerBallList;
		public PositionedObjectList<PlayerBall> PlayerBallList
		{
			get
			{
				return mPlayerBallList;
			}
		}
		private MP3.Entities.PlayerBall mPlayerBallInstances;
		public MP3.Entities.PlayerBall PlayerBallInstances
		{
			get
			{
				return mPlayerBallInstances;
			}
		}
		private MP3.Entities.PlayerBall mPlayerBallInstance2;
		public MP3.Entities.PlayerBall PlayerBallInstance2
		{
			get
			{
				return mPlayerBallInstance2;
			}
		}
		private FlatRedBall.Math.Geometry.AxisAlignedRectangle LeftGoal;
		private FlatRedBall.Math.Geometry.AxisAlignedRectangle RightGoal;
		private FlatRedBall.Graphics.Layer HudLayer;
		private MP3.Entities.ScoreHUD mScoreHUDInstance;
		public MP3.Entities.ScoreHUD ScoreHUDInstance
		{
			get
			{
				return mScoreHUDInstance;
			}
		}
		private PositionedObjectList<Puck> mPuckList;
		public PositionedObjectList<Puck> PuckList
		{
			get
			{
				return mPuckList;
			}
		}
		private MP3.Entities.Puck Puck1;
		private MP3.Entities.Puck Puck5;
		private MP3.Entities.Puck Puck4;
		private MP3.Entities.Puck Puck3;
		private MP3.Entities.Puck Puck2;
		private PositionedObjectList<PowerUp> PowerUpList;
		private MP3.Entities.PowerUp PowerUp1;
		private MP3.Entities.PowerUp PowerUp2;
		private MP3.Entities.LabelHUD LabelHUDInstance;

		public GameScreen()
			: base("GameScreen")
		{
		}

        public override void Initialize(bool addToManagers)
        {
			// Generated Initialize
			LoadStaticContent(ContentManagerName);
			if (!FlatRedBallServices.IsLoaded<ShapeCollection>(@"content/screens/gamescreen/collisionfile.shcx", ContentManagerName))
			{
			}
			CollisionFile = FlatRedBallServices.Load<ShapeCollection>(@"content/screens/gamescreen/collisionfile.shcx", ContentManagerName);
			if (!FlatRedBallServices.IsLoaded<ShapeCollection>(@"content/screens/gamescreen/goalareafile.shcx", ContentManagerName))
			{
			}
			GoalAreaFile = FlatRedBallServices.Load<ShapeCollection>(@"content/screens/gamescreen/goalareafile.shcx", ContentManagerName);
			if (!FlatRedBallServices.IsLoaded<ShapeCollection>(@"content/screens/gamescreen/top2walls.shcx", ContentManagerName))
			{
			}
			Top2Walls = FlatRedBallServices.Load<ShapeCollection>(@"content/screens/gamescreen/top2walls.shcx", ContentManagerName);
			mPlayerBallList = new PositionedObjectList<PlayerBall>();
			mPlayerBallInstances = new MP3.Entities.PlayerBall(ContentManagerName, false);
			mPlayerBallInstances.Name = "mPlayerBallInstances";
			mPlayerBallInstance2 = new MP3.Entities.PlayerBall(ContentManagerName, false);
			mPlayerBallInstance2.Name = "mPlayerBallInstance2";
			LeftGoal = GoalAreaFile.AxisAlignedRectangles.FindByName("LeftGoal");
			RightGoal = GoalAreaFile.AxisAlignedRectangles.FindByName("RightGoal");
			HudLayer = new FlatRedBall.Graphics.Layer();
			HudLayer.Name = "HudLayer";
			mScoreHUDInstance = new MP3.Entities.ScoreHUD(ContentManagerName, false);
			mScoreHUDInstance.Name = "mScoreHUDInstance";
			mPuckList = new PositionedObjectList<Puck>();
			Puck1 = new MP3.Entities.Puck(ContentManagerName, false);
			Puck1.Name = "Puck1";
			Puck5 = new MP3.Entities.Puck(ContentManagerName, false);
			Puck5.Name = "Puck5";
			Puck4 = new MP3.Entities.Puck(ContentManagerName, false);
			Puck4.Name = "Puck4";
			Puck3 = new MP3.Entities.Puck(ContentManagerName, false);
			Puck3.Name = "Puck3";
			Puck2 = new MP3.Entities.Puck(ContentManagerName, false);
			Puck2.Name = "Puck2";
			PowerUpList = new PositionedObjectList<PowerUp>();
			PowerUp1 = new MP3.Entities.PowerUp(ContentManagerName, false);
			PowerUp1.Name = "PowerUp1";
			PowerUp2 = new MP3.Entities.PowerUp(ContentManagerName, false);
			PowerUp2.Name = "PowerUp2";
			LabelHUDInstance = new MP3.Entities.LabelHUD(ContentManagerName, false);
			LabelHUDInstance.Name = "LabelHUDInstance";
			PlayerBallList.Add(PlayerBallInstances);
			PlayerBallList.Add(PlayerBallInstance2);
			PuckList.Add(Puck1);
			PuckList.Add(Puck5);
			PuckList.Add(Puck4);
			PuckList.Add(Puck3);
			PuckList.Add(Puck2);
			PowerUpList.Add(PowerUp1);
			PowerUpList.Add(PowerUp2);
			
			
			PostInitialize();
			base.Initialize(addToManagers);
			if (addToManagers)
			{
				AddToManagers();
			}

        }
        
// Generated AddToManagers
		public override void AddToManagers ()
		{
			SpriteManager.AddLayer(HudLayer);
			HudLayer.UsePixelCoordinates();
			base.AddToManagers();
			AddToManagersBottomUp();
			CustomInitialize();
		}


		public override void Activity(bool firstTimeCalled)
		{
			// Generated Activity
			if (!IsPaused)
			{
				
				for (int i = PlayerBallList.Count - 1; i > -1; i--)
				{
					if (i < PlayerBallList.Count)
					{
						// We do the extra if-check because activity could destroy any number of entities
						PlayerBallList[i].Activity();
					}
				}
				ScoreHUDInstance.Activity();
				for (int i = PuckList.Count - 1; i > -1; i--)
				{
					if (i < PuckList.Count)
					{
						// We do the extra if-check because activity could destroy any number of entities
						PuckList[i].Activity();
					}
				}
				for (int i = PowerUpList.Count - 1; i > -1; i--)
				{
					if (i < PowerUpList.Count)
					{
						// We do the extra if-check because activity could destroy any number of entities
						PowerUpList[i].Activity();
					}
				}
				LabelHUDInstance.Activity();
			}
			else
			{
			}
			base.Activity(firstTimeCalled);
			if (!IsActivityFinished)
			{
				CustomActivity(firstTimeCalled);
			}


				// After Custom Activity
				
            
		}

		public override void Destroy()
		{
			// Generated Destroy
			if (this.UnloadsContentManagerWhenDestroyed)
			{
				CollisionFile.RemoveFromManagers(ContentManagerName != "Global");
			}
			else
			{
				CollisionFile.RemoveFromManagers(false);
			}
			if (this.UnloadsContentManagerWhenDestroyed)
			{
				GoalAreaFile.RemoveFromManagers(ContentManagerName != "Global");
			}
			else
			{
				GoalAreaFile.RemoveFromManagers(false);
			}
			if (this.UnloadsContentManagerWhenDestroyed)
			{
				Top2Walls.RemoveFromManagers(ContentManagerName != "Global");
			}
			else
			{
				Top2Walls.RemoveFromManagers(false);
			}
			
			for (int i = PlayerBallList.Count - 1; i > -1; i--)
			{
				PlayerBallList[i].Destroy();
			}
			if (LeftGoal != null)
			{
				LeftGoal.Detach(); ShapeManager.Remove(LeftGoal);
			}
			if (RightGoal != null)
			{
				RightGoal.Detach(); ShapeManager.Remove(RightGoal);
			}
			if (HudLayer != null)
			{
				SpriteManager.RemoveLayer(HudLayer);
			}
			if (ScoreHUDInstance != null)
			{
				ScoreHUDInstance.Destroy();
				ScoreHUDInstance.Detach();
			}
			for (int i = PuckList.Count - 1; i > -1; i--)
			{
				PuckList[i].Destroy();
			}
			for (int i = PowerUpList.Count - 1; i > -1; i--)
			{
				PowerUpList[i].Destroy();
			}
			if (LabelHUDInstance != null)
			{
				LabelHUDInstance.Destroy();
				LabelHUDInstance.Detach();
			}

			base.Destroy();

			CustomDestroy();

		}

		// Generated Methods
		public virtual void PostInitialize ()
		{
			if (PlayerBallInstances.Parent == null)
			{
				PlayerBallInstances.X = 10f;
			}
			else
			{
				PlayerBallInstances.RelativeX = 10f;
			}
			if (PlayerBallInstance2.Parent == null)
			{
				PlayerBallInstance2.X = -10f;
			}
			else
			{
				PlayerBallInstance2.RelativeX = -10f;
			}
			PowerUp1.BodyColor = Color.Tomato;
			PowerUp2.BodyColor = Color.Orange;
		}
		public virtual void AddToManagersBottomUp ()
		{
			CollisionFile.AddToManagers(mLayer);
			GoalAreaFile.AddToManagers(mLayer);
			Top2Walls.AddToManagers(mLayer);
			mPlayerBallInstances.AddToManagers(mLayer);
			if (mPlayerBallInstances.Parent == null)
			{
				mPlayerBallInstances.X = 10f;
			}
			else
			{
				mPlayerBallInstances.RelativeX = 10f;
			}
			mPlayerBallInstances.CurrentState = MP3.Entities.PlayerBall.VariableState.Stationary;
			mPlayerBallInstance2.AddToManagers(mLayer);
			if (mPlayerBallInstance2.Parent == null)
			{
				mPlayerBallInstance2.X = -10f;
			}
			else
			{
				mPlayerBallInstance2.RelativeX = -10f;
			}
			mPlayerBallInstance2.CurrentState = MP3.Entities.PlayerBall.VariableState.Stationary;
			mScoreHUDInstance.AddToManagers(HudLayer);
			Puck1.AddToManagers(mLayer);
			Puck5.AddToManagers(mLayer);
			Puck4.AddToManagers(mLayer);
			Puck3.AddToManagers(mLayer);
			Puck2.AddToManagers(mLayer);
			PowerUp1.AddToManagers(mLayer);
			PowerUp1.BodyColor = Color.Tomato;
			PowerUp2.AddToManagers(mLayer);
			PowerUp2.BodyColor = Color.Orange;
			LabelHUDInstance.AddToManagers(HudLayer);
		}
		public virtual void ConvertToManuallyUpdated ()
		{
			for (int i = 0; i < PlayerBallList.Count; i++)
			{
				PlayerBallList[i].ConvertToManuallyUpdated();
			}
			ScoreHUDInstance.ConvertToManuallyUpdated();
			for (int i = 0; i < PuckList.Count; i++)
			{
				PuckList[i].ConvertToManuallyUpdated();
			}
			for (int i = 0; i < PowerUpList.Count; i++)
			{
				PowerUpList[i].ConvertToManuallyUpdated();
			}
			LabelHUDInstance.ConvertToManuallyUpdated();
		}
		public static void LoadStaticContent (string contentManagerName)
		{
			#if DEBUG
			if (contentManagerName == FlatRedBallServices.GlobalContentManager)
			{
				HasBeenLoadedWithGlobalContentManager = true;
			}
			else if (HasBeenLoadedWithGlobalContentManager)
			{
				throw new Exception("This type has been loaded with a Global content manager, then loaded with a non-global.  This can lead to a lot of bugs");
			}
			#endif
			MP3.Entities.ScoreHUD.LoadStaticContent(contentManagerName);
			MP3.Entities.LabelHUD.LoadStaticContent(contentManagerName);
			CustomLoadStaticContent(contentManagerName);
		}
		object GetMember (string memberName)
		{
			switch(memberName)
			{
				case  "CollisionFile":
					return CollisionFile;
				case  "GoalAreaFile":
					return GoalAreaFile;
				case  "Top2Walls":
					return Top2Walls;
			}
			return null;
		}


	}
}
